import {defineStore} from "pinia";
import {ref} from "vue";
import {getBoardListApi} from "@/api/boardApi.js";

export const useBoardStore = defineStore('boardStore', () => {
    let firstBoard = ref([]);
    let secondBoard = ref([]);
    let boardList = ref([])
    const getBoardList = async () => {
        // 发起请求前判断session中是否有数据
        if (boardList.value.length > 0) return

        // 向服务端获取板块数据
        let response = await getBoardListApi();
        firstBoard.value = response.data.filter(board => {
            return board.children.length > 0
        })
        secondBoard.value = response.data.filter(board => {
            return board.children.length <= 0
        })
        boardList.value = response.data
    }
    return {
        firstBoard, secondBoard, boardList,
        getBoardList
    }

}, {
    // 将板块数据持久化到session
    persist: {
        storage: sessionStorage
    }
})